<?php


namespace app\hapi\controller;


use app\common\model\Parkinfo;
use app\common\model\Sysuser;
use think\Controller;

class Sysusers extends ApiBase
{
    public function getuserlist($param)
    {

        $query = json_decode($param['query'], true);
        $page = ($query['page'] - 1) * $query['limit'];
        $map = [];
        if (isset($query['name'])) {
            $map['a.name'] = $query['name'];
        }
        $Sysuser = new Sysuser();
        $list = $Sysuser
            ->alias('a')
            ->join('wn_auth_group b','a.group = b.id','left')
            ->where($map)->limit($page, $query['limit'])
            ->field('a.id,a.name,a.username,a.group,a.create_user,a.create_time,b.title as groupname')
            ->order('id desc')->select();
        $count = $Sysuser->alias('a')->where($map)->count();
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $list;
        $re_data['data']['total'] = $count;
        return $re_data;
    }

    public function sysuseradd($param)
    {
        $query = json_decode($param['query'], true);
        $data['name'] = $query['name'];
        $data['username'] = $query['username'];
        $data['salt'] = generateNonceStr(8);
        $data['password'] = MD5($query['password'] . $data['salt']);
        $data['group'] = $query['group'];
        $data['create_user'] = "";
        $data['create_time'] = time();
        $add = Sysuser::create($data);
        if ($add) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        }
        return $re_data;
    }

    public function sysuseredit($param)
    {
        $query = json_decode($param['query'], true);
        $data['id'] = $query['id'];
        $data['name'] = $query['name'];
        $data['username'] = $query['username'];
        if (!empty($query['password'])) {

            $data['salt'] = generateNonceStr(8);
            $data['password'] = MD5($query['password'] . $data['salt']);
        }
        $data['group'] = $query['group'];
        $add = Sysuser::update($data);
        if ($add) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        }
        return $re_data;
    }
    public function sysuserdel($param)
    {
        $query = json_decode($param['query'], true);
        $del = Sysuser::destroy(['id' => $query['id']]);
        if ($del) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "删除失败";
        }
        return $re_data;
    }

}